package com.imooc.momo_app;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * @ClassName: UIDemoActivity
 * @Author: Jie Li
 * @Date 2019-11-21 17:21
 * @Description: TODO
 * @Version: 1.0
 * <p>Copyright: Copyright (c) 2019</p>
 **/
public class UIDemoActivity extends AppCompatActivity {
    private Button startOther, uilook;
    private TextView textView, uiprice;
    private RadioButton rb_man, rb_woman;
    private CheckBox la, hx, suan;
    private ButtonsListener buttonsListener;
    private CheckBoxListener checkBoxListener;
    private SeekBar seekBar;
    private ImageView imageView;
    private StringBuffer sb = new StringBuffer();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.ui_demo_layout);
        //启动另外一个activity
        startOther = findViewById(R.id.uidemobutton);
        startOther.setOnClickListener(new UIDemoActivity.StartListener());

        //按钮
        ButtonsListenerLook buttonsListenerLook = new ButtonsListenerLook();
        uilook = findViewById(R.id.uilook);
        uilook.setOnClickListener(buttonsListenerLook);
        //性别
        buttonsListener = new ButtonsListener();
        rb_woman = findViewById(R.id.rb_woman);
        rb_man = findViewById(R.id.rb_man);
        rb_man.setOnClickListener(buttonsListener);
        rb_woman.setOnClickListener(buttonsListener);
        //喜好
        checkBoxListener = new CheckBoxListener();
        initView();
        setListener();

        //seekBar
        seekBar = findViewById(R.id.sb_price);
        // 设置进度条最大值
        seekBar.setMax(100);
        // 设置进度条当前值
        seekBar.setProgress(0);
        uiprice = findViewById(R.id.uiprice);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @SuppressLint("SetTextI18n")
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // 将在结束拖拽进度条时被触发
                uiprice.setText(seekBar.getProgress() + "元");
                sb.append("进度条"+seekBar.getProgress());
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // 将在开始拖拽进度条时被触发
                Log.i("progress", "开始 " + seekBar.getProgress() + "");
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                                          boolean fromUser) {
                /*
                 * 将在进度发生变化时被触发 1、当前绑定的seekBar对象 2、当前进度数值 3、是否为用户手动触发
                 */
                Log.i("progress", seekBar.getProgress() + "");
            }
        });

    }

    private void initView() {
        la = findViewById(R.id.la);
        hx = findViewById(R.id.hx);
        suan = findViewById(R.id.suan);
    }

    private void setListener() {
        checkBoxListener = new CheckBoxListener();
        la.setOnCheckedChangeListener(checkBoxListener);
        hx.setOnCheckedChangeListener(checkBoxListener);
        suan.setOnCheckedChangeListener(checkBoxListener);
    }

    class ButtonsListenerLook implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            //姓名
            textView = findViewById(R.id.uiname);
            System.out.println(textView.getText().toString());
            sb.append("选中:");
            sb.append(textView.getText());
            //图片
            imageView = findViewById(R.id.imageView);
            imageView.setImageResource(R.drawable.unhappy);
            System.out.println(sb.toString());
        }
    }

    class CheckBoxListener implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // 当选中状态发生变化时触发
            CheckBox checkBox = (CheckBox) buttonView;
            switch (checkBox.getId()) {
                case R.id.la:
                    if (isChecked) {
                        sb.append(checkBox.getText().toString());
                    }
                    break;
                case R.id.hx:
                    if (isChecked) {
                        sb.append(checkBox.getText().toString());
                    }
                    break;
                case R.id.suan:
                    if (isChecked) {
                        sb.append(checkBox.getText().toString());
                    }
                    break;

            }
        }
    }

    class ButtonsListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.rb_man:
                    sb.append(rb_man.getText().toString());
                    break;
                case R.id.rb_woman:
                    sb.append(rb_woman.getText().toString());
                    break;
            }
        }
    }

    class StartListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            // 当点击事件触发后执行，启动OtherActivity
            //创建一个Intent对象
            Intent intent = new Intent();
            //调用setClass方法指定启动某一个Activity
            intent.setClass(UIDemoActivity.this, MainActivity.class);
            //调用startActivity
            startActivity(intent);

        }

    }
}
